#include "logon.h"
#include "train.h"
int logon(int netfd,char*arr,MYSQL *conn){                                  
    char temp_sql[128]={0};
    snprintf(temp_sql,sizeof(temp_sql),"SELECT * FROM user WHERE user_name=='%s'",arr);
    int res = is_existed(conn,temp_sql);
    train_t train;
    if(res == 0){
        char salt_buf[30]={0};
        MYSQL_RES *table=NULL;
        char temp_sql[128]={0};
        snprintf(temp_sql,sizeof(temp_sql),"SELECT user_salt FROM user WHERE user_name=='%s'",arr);
        db_execute(conn,temp_sql,&table);
        MYSQL_ROW salt = mysql_fetch_row(table);
        printf("user_name=%s\n",salt[0]);
        memcpy(salt_buf,salt[0],strlen(salt[0]));
        train.type =11;
        train.length=strlen(salt_buf);
        memcpy(train.value,salt_buf,strlen(salt_buf));
        send(netfd,&train.type,4,MSG_NOSIGNAL);
        send(netfd,&train.length,4,MSG_NOSIGNAL);
        send(netfd,train.value,train.length,MSG_NOSIGNAL);
        char encrypted_password[256]={0};
        handle_command(netfd,encrypted_password);
        printf("encrypted_password=%s\n",encrypted_password);
        char temp_sql_encrypted_passwd[128]={0};
        snprintf(temp_sql_encrypted_passwd,sizeof(temp_sql_encrypted_passwd),"SELECT * FROM user WHERE encrypted_password=='%s'",arr);                    
        int passwd_existed=is_existed(conn,temp_sql_encrypted_passwd);
        if(passwd_existed!=0){
            return -1;
        }
    }
    return 0;
}

